package com.winter.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.google.common.base.Strings;

import com.winter.mapper.FriendMapper;
import com.winter.mapper.UserInfoMapper;
import com.winter.model.Friend;
import com.winter.model.UserCompanyBean;
import com.winter.model.UserInfo;
import com.winter.util.Constants;
import com.winter.util.Result;
import com.winter.util.ResultCode;
import com.winter.util.UUIDUitls;

@Service
public class FriendService {

    @Resource
    private FriendMapper friendMapper;
    @Resource
    private UserInfoMapper userInfoMapper;

    /*
     * 添加好友
     *
     */
    public Result saveFriend(Friend friend) {
        if (null != friend) {
            if (Strings.isNullOrEmpty(friend.getUserNo())) {
                return Result.failure(ResultCode.USER_NOT_LOGGED_IN);
            }
            UserInfo uit = userInfoMapper.selectByPrimaryKey(Long.valueOf(friend.getUserNo()));
            if (uit.getAuditFlg()
                   .equals("0")) {
                return Result.failure(ResultCode.FAILURE, "请先进行审核");
            }
            if (friend.getCompanyUser() != null && friend.getUserNo() != null) {
                if (friend.getUserNo()
                          .equals(friend.getCompanyUser())) {
                    return Result.failure(ResultCode.FAILURE, "不能添加自己为好友");
                }
                Friend fd = friendMapper.selectByFriendUser(friend.getUserNo(), friend.getCompanyUser());
                if (null != fd) {
                    return Result.failure(ResultCode.FAILURE, "已申请过好友,请勿重新申请好友");
                }
            }
            if (uit.getIsjrqmember()
                   .equals("1")) {
                List<Friend> fdList = friendMapper.findFriendList(friend.getUserNo());
                if (fdList.size() >= 5) {
                    return Result.failure(ResultCode.FAILURE, "金融圈会员最多加5个好友,如需添加更好好友,请升级为VIP");
                }
            }
            //自动添加好友status(1.待审核；2已申请；3已完成)
            friend.setId(UUIDUitls.uuid());
            friend.setCreateDate(new Date());
            friend.setUpdateDate(new Date());
            friend.setCreateUser(friend.getUserNo());
            friend.setUpdateUser(friend.getUserNo());
            friend.setStatus("2");
            friendMapper.insertSelective(friend);
            //再生成一条被添加好友的记录
            Friend friend2 = new Friend();
            friend2.setId(UUIDUitls.uuid());
            friend2.setCreateDate(new Date());
            friend2.setUpdateDate(new Date());
            friend2.setCreateUser(friend.getCompanyUser());
            friend2.setUpdateUser(friend.getCompanyUser());
            friend2.setStatus("1");
            friend2.setCompanyUser(friend.getUserNo());
            friend2.setUserNo(friend.getCompanyUser());
            friendMapper.insertSelective(friend2);
            return Result.success("成功");
        }
        return null;
    }

    /*
     *
     * 只有status=3才能删除好友
     */
    public Result deleteFriend(String userNo, String companyUser) {
        if (Strings.isNullOrEmpty(userNo)) {
            return Result.failure(ResultCode.FAILURE, "用户未登录,请登录");
        }
        Friend fd = friendMapper.selectByFriendUser(userNo, companyUser);
        if (null != fd && fd.getStatus()
                            .equals("3")) {
            friendMapper.deleteByPrimaryKey(fd.getId());
            Friend fd2 = friendMapper.selectByFriendUser(companyUser, userNo);
            if (null != fd2 && fd2.getStatus()
                                  .equals("3")) {
                friendMapper.deleteByPrimaryKey(fd2.getId());
            }
        }
        return Result.success("成功");

    }

    /*
     *
     * 只有status=1才能审核好友
     */
    public Result sendFriend(String userNo, String companyUser) {
        if (Strings.isNullOrEmpty(userNo)) {
            return Result.failure(ResultCode.FAILURE, "用户未登录,请登录");
        }

        Friend fd = friendMapper.selectByFriendUser(userNo, companyUser);
        if (null != fd && fd.getStatus()
                            .equals("1")) {
            fd.setStatus("3");
            friendMapper.updateByPrimaryKeySelective(fd);
            Friend fd2 = friendMapper.selectByFriendUser(companyUser, userNo);
            if (null != fd2 && fd2.getStatus()
                                  .equals("2")) {
                fd2.setStatus("3");
                friendMapper.updateByPrimaryKeySelective(fd2);
            }
        }
        return Result.success("成功");
    }

    /*
     * 我的好友列表
     *
     */

    public Result friendList(Integer pageNumber, Integer pageSize, String userNo) {
        if (pageNumber == null) {
            pageNumber = Constants.PAGENUMBER;
        }
        if (pageSize == null) {
            pageSize = Constants.PAGESIZE;
        }
        PageHelper.startPage(pageNumber, pageSize, false);
        List<UserCompanyBean> comList = friendMapper.friendList(userNo);
        PageInfo<UserCompanyBean> pageInfo = new PageInfo<>(comList);
        return Result.success(pageInfo);

    }
}
